
//
const app = getApp();
//
let posterImgSrc = "";  //生成的海报图片链接
let showPosterPop = true; //海报图片弹层
let designWid = 375;  //设计图宽度
let edgeSize = 16;    //设计图海报的边距
let proWid = 303;  //设计图--产品图宽度
let proH = 303;  //设计图--产品图高度
let finalWid = 335;  //生成的图片宽度
let finalH = 502;  //生成的图片高度
/*  */
let initX = 0;  //绘制时的坐标原点-x
let initY = 0;  //绘制时的坐标原点-y
/*  */
let contentWid = 0;  //实际绘制的内容区宽度
/* 计算边距 + 内容区宽度 */
const calcEdgesize = function(sw){
  const ces = Math.floor((sw/designWid)*edgeSize);
  this.setData({
    contentWid:sw - ces*2
  });
  return ces;
};
/* 换算一下 */
const transferSize = function(os){
  return Math.floor((app.globalData.windowWidth/designWid)*os);
};
/* 
绘制图片 
isFull: 是否和内容区一样宽
*/
const paintImg = function(imgSrc,x,y,isFull,dx,dy){
  console.log(imgSrc);
  let w,h;
  //
  if(isFull){
    w = this.data.contentWid
  }else{
    w = Math.floor((app.globalData.windowWidth/designWid)*proWid);
  }
  h =Math.floor((app.globalData.windowWidth/designWid)*proH);
  //指定图片尺寸
  if(dx&&dy){
    w = Math.floor((app.globalData.windowWidth/designWid)*dx);
    h = Math.floor((app.globalData.windowWidth/designWid)*dy);
  }
  //
  console.log(w,h);
  return new Promise((resolve, reject) => {
    wx.getImageInfo({
      src: imgSrc,
      success: res => {
        console.log(res);
        //防止图片变形
        let h = Math.floor(w/(res.width/res.height));
        if(h>proH){
          h = proH;
        }
        // 
        this.data.ctx.drawImage(`${res.path}`, x, y, w, h);
        this.data.ctx.draw(true);
        //坐标偏移
        resolve({
          w:w,
          h:h
        });
      },
      fail: err => {
        console.log(err);
        reject('绘制图片失败');
      }
    });
  });
};
/* 
  绘制文字
  str:要绘制的文字内容
  fs:文字大小
  x: x坐标
  y: y坐标
  max: 一行最多绘制多少个文字
  color:文字颜色
*/
const paintTxt = function(str,fs,x,y,max,color){
  try {
    if(str.length>max){
      str = str.substr(0,max)+"...";
    }
    this.data.ctx.setFontSize(fs);
    this.data.ctx.fillStyle = color;
    this.data.ctx.fillText(str, x, y);
    this.data.ctx.draw(true);
    const metrics = this.data.ctx.measureText(str)
    console.log(metrics);
    return Promise.resolve({
      w:metrics.width,
      h:fs
    })
  } catch (error) {
    return Promise.reject(error);
  }
};
/* 画线 */
const paintLine = function(x,y,x2,y2,lw,color){
  try {
    this.data.ctx.moveTo(x, y);
    this.data.ctx.lineTo(x2, y2);
    this.data.ctx.setLineWidth(lw);
    this.data.ctx.strokeStyle = color;
    this.data.ctx.stroke();
    this.data.ctx.draw(true);
    return Promise.resolve({
      w:x2 - x,
      h:lw
    })
  } catch (error) {
    return Promise.reject(error);
  }
};
/* 获取canvas绘制的图片 */
const getPosterImgUrl = function(canvasId){
  let w,h;
  //w = Math.floor((app.globalData.windowWidth/designWid)*finalWid);
  w = app.globalData.windowWidth;
  h = Math.floor((app.globalData.windowWidth/designWid)*finalH);
  return new Promise((resolve,reject)=>{
    let timer = null;
    clearTimeout(timer);
    timer = setTimeout(() => {
      //
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        canvasId: canvasId,
        width: w,
        height: h,
        //fileType: 'jpg',
        success: res => {
          console.log(res);
          resolve(res.tempFilePath);
        },
        fail: err=> {
          console.log(err)
          reject(err)
        },
        complete: () => {
          
        }
      });
    }, 200);
  });
};
//
module.exports = {
  posterImgSrc,
  showPosterPop,
  designWid,
  proWid,
  proH,
  finalWid,
  finalH,
  edgeSize,
  contentWid,
  initX,
  initY,
  /*  */
  calcEdgesize,
  /*  */
  transferSize,
  /*  */
  paintImg,
  /*  */
  paintTxt,
  /*  */
  paintLine,
  /*  */
  getPosterImgUrl
};